﻿/*
 * @file: NewsBLL.cs
 * @in namespace: BLL
 */

using System;
using DAL;

/// <summary>
/// Summary description for NewsBLL
/// </summary>
namespace BLL
{
    public class NewsBLL
    {
        public NewsBLL()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region INews Members

        /// <summary>
        /// GetNews
        /// </summary>
        /// <param name="allowFilter">"true" if allow ignore hidden records</param>
        /// <returns>Records corresponding</returns>
        public static News[] GetNews(bool allowFilter)
        {
            try
            {
                return DataAccessHelper.GetNewsDA().GetNews(allowFilter);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// GetNewsByNewsID
        /// </summary>
        /// <param name="newsID">News ID</param>
        /// <param name="allowFilter">"true" if allow ignore hidden records</param>
        /// <returns>Record corresponding</returns>
        public static News GetNewsByNewsID(int newsID, bool allowFilter)
        {
            try
            {
                return DataAccessHelper.GetNewsDA().GetNewsByNewsID(newsID, allowFilter);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// DisableNews
        /// </summary>
        /// <param name="newsID">News ID</param>
        /// <param name="isEnable">"true" if allow enable News, "false" if allow hidden News</param>
        /// <returns>1 if successful, 0 if error occurred</returns>
        public static int DisableNews(int newsID, bool isEnable)
        {
            try
            {
                return DataAccessHelper.GetNewsDA().DisableNews(newsID, isEnable);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// InsertNews
        /// </summary>
        /// <param name="_news">News entity</param>
        /// <param name="autoNumber">Current News ID inserted</param>
        /// <returns>1 if successful, 0 if error occurred</returns>
        public static int InsertNews(News _news, out int autoNumber)
        {
            try
            {
                if (_news.Title == null || _news.Title.Length < 0)
                    throw new OnlineFloralDeliveryException(2, "Title is not allow null or less than 0");
                else if (NewsDA.TitleExisted(_news.Content) > 0)
                    throw new OnlineFloralDeliveryException(1, "Title already exist");
                else if (_news.Content == null || _news.Content.Length == 0)
                    throw new OnlineFloralDeliveryException(2, "Content is not allow null");

                return DataAccessHelper.GetNewsDA().InsertNews(_news, out autoNumber);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// UpdateNews
        /// </summary>
        /// <param name="_news">News entity</param>
        /// <returns>1 if successful, 0 if error occurred</returns>
        public static int UpdateNews(News _news)
        {
            try
            {
                if (_news.NewsID == 0)
                    throw new OnlineFloralDeliveryException(2, "NewsID is not allow null or less than 0");
                else if (_news.Title == null || _news.Title.Length < 0)
                    throw new OnlineFloralDeliveryException(2, "Title is not allow null or empty");
                else if (NewsDA.TitleExisted(_news.Content) > 0)
                    throw new OnlineFloralDeliveryException(1, "Title already exist");
                else if (_news.Content == null || _news.Content.Length == 0)
                    throw new OnlineFloralDeliveryException(2, "Content is not allow null or empty");
                else if (_news.PostedDate == null)
                    throw new OnlineFloralDeliveryException(2, "PostedDate is not allow null or empty");

                return DataAccessHelper.GetNewsDA().UpdateNews(_news);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}